import DataAdapter from "./gantt.adapter.js"
import { generateUUID } from "./utils/uuid.js"

export const projects = [
    {
        id: generateUUID("pid"),
        label: "S",
        plans: [{
            id: generateUUID("plan"),
            start: "2024-02-07",
            end: "2024-02-25",
        }, {
            id: generateUUID("plan"),
            start: "2024-06-01",
            end: "2024-07-20",
        }],
        children: [{
            id: generateUUID("pid"),
            label: "s-1",
            plans: [{
                id: generateUUID("plan"),
                start: "2024-03-01",
                end: "2024-03-20",
            }, {
                id: generateUUID("plan"),
                start: "2024-04-01",
                end: "2024-05-20",
            }]

        }]

    },
    {
        id: generateUUID("pid"),
        label: "A",
        plans: [{
            id: generateUUID("plan"),
            start: "2024-02-09",
            end: "2024-03-10",
        }, {
            id: generateUUID("plan"),
            start: "2024-04-01",
            end: "2024-05-20",
        }],
        children: [{
            id: generateUUID("pid"),
            label: "A-1",
            plans: [
                {
                    id: generateUUID("plan"),
                    start: "2024-05-01",
                    end: "2024-06-30",
                }
            ],
            children: [{
                id: generateUUID("pid"),
                label: "A-1-1",
                plans: [{
                    id: generateUUID("plan"),
                    start: "2024-07-01",
                    end: "2024-07-30",
                }]

            }]
        }, {
            id: generateUUID("pid"),
            label: "A-2",
            plans: [
                {
                    id: generateUUID("plan"),
                    start: "2024-08-01",
                    end: "2024-08-20",
                }
            ]
        }]
    }, {
        id: generateUUID("pid"),
        label: "B",
    }, {
        id: generateUUID("pid"),
        label: "C",
    }, {
        id: generateUUID("pid"),
        label: "D",
    }, {
        id: generateUUID("pid"),
        label: "E",
    }, {
        id: generateUUID("pid"),
        label: "F",
    }, {
        id: generateUUID("pid"),
        label: "G",
    }

]


function getMaxLevel(datas, count) {
    count = count || 0
    if (!Array.isArray(datas) || datas.length < 1) return count

    count += 1
    let maxCount = count
    datas.forEach(d => {
        let c = getMaxLevel(d.children, count)
        if (c > maxCount) {
            maxCount = c
        }

    })
    return maxCount
}


function getDescendantPlans(datas) {
    if (!Array.isArray(datas) || datas.length < 1) return []
    let plans = []
    datas.forEach(d => {
        let p1 = d.plans || []
        let p2 = getDescendantPlans(d.children)
        plans = plans.concat(p1).concat(p2)
    })
    return plans
}

function getlevelPlansInner(datas, stores = [], targetLevel, currentLevel) {
    currentLevel = currentLevel || 0
    if (!Array.isArray(datas) || datas.length < 1) return
    if (currentLevel > targetLevel) return
    currentLevel += 1
    let hit = currentLevel === targetLevel
    datas.forEach(d => {
        let p1 = [].concat(d.plans || [])
        if (hit) {
            let p2 = getDescendantPlans(d.children)
            stores.push({
                key: d.id,
                values: p1.concat(p2)
            })
        } else {
            stores.push({
                key: d.id,
                values: p1
            })
            getlevelPlansInner(d.children, stores, targetLevel, currentLevel)
        }
    })
}

function getLevelPlans(datas, targetLevel) {
    let saveCache = []
    getlevelPlansInner(datas, saveCache, targetLevel)
    return saveCache
}


function parseProjectsByK(datas) {
    let maxLevel = getMaxLevel(projects)
    let rlts = []
    for (let i = 1; i <= maxLevel; i++) {
        let arr = getLevelPlans(projects, i)
        // console.log("level=%s", i, JSON.stringify(arr))
        rlts[i - 1] = arr
    }

    return rlts
}

export const series = parseProjectsByK(projects)

// console.log("series", series)
// setInterval(() => { }, 20000)


// let adapter = new DataAdapter()
// adapter.data = projects
// console.log("options1", adapter.options)
// adapter.openKey("A")
// console.log("options2", adapter.options)
// adapter.openKey("A-1")
// console.log("options3", adapter.options)
// adapter.closeKey("A")
// console.log("options4", adapter.options)
// adapter.openKey("A")
// console.log("options5", adapter.options)

// adapter.data = projects
// console.log("options6", adapter.options)